package com.zlyx.easy.core.buffer;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import com.zlyx.easy.core.jackson.JSONObject;
import com.zlyx.easy.core.utils.ObjectUtils;

/**
 * 
 * @Auth 赵光
 * @Describle StringBuilder的增强形态
 * @2018年12月28日 下午4:20:25
 */
public final class EasyBuffer {

	private StringBuilder easyBuffer = null;

	public EasyBuffer() {
		this(null);
	}

	public EasyBuffer(Object o) {
		this(new StringBuilder(String.valueOf(o)));
	}

	public EasyBuffer(StringBuilder easyBuffer) {
		this.easyBuffer = easyBuffer;
		if (this.easyBuffer == null) {
			this.easyBuffer = new StringBuilder();
		}
	}

	public EasyBuffer append(Object o) {
		this.easyBuffer.append(o);
		return this;
	}

	public EasyBuffer append(Object... objs) {
		append(Arrays.asList(objs));
		return this;
	}

	public EasyBuffer append(Collection<?> c) {
		if (c == null) {
			return this;
		}
		for (Object obj : c) {
			append(obj);
		}
		return this;
	}

	public boolean contains(String s) {
		return this.easyBuffer.toString().indexOf(s) >= 0;
	}

	@Override
	public boolean equals(Object o) {
		return toString().equals(o);
	}

	public EasyBuffer replace(String placeholder, String replacement) {
		String s = toString();
		if (s.contains(placeholder)) {
			this.easyBuffer = new StringBuilder(s.replace(placeholder, replacement));
		}
		return this;
	}

	/**
	 * 去掉从指定位置开始的字符
	 * 
	 * @param i
	 * @return
	 */
	public String clearEnd(int i) {
		String tmp = this.toString().trim();
		if (tmp.length() >= i) {
			tmp = tmp.substring(0, tmp.length() - i);
		}
		return tmp;
	}

	/**
	 * 去掉最后一个字符
	 * 
	 * @return
	 */
	public String clearEnd() {
		return clearEnd(1);
	}

	/**
	 * 删除指定大小的片段(头部)
	 * 
	 * @param i
	 * @return
	 */
	public String substring(int i) {
		if (this.toString().length() < i) {
			return this.toString();
		}
		return this.toString().substring(i);
	}

	/**
	 * 删除指定大小的片段(尾部)
	 * 
	 * @param end
	 * @return
	 */
	public EasyBuffer delete(int size) {
		if (this.easyBuffer.length() >= size) {
			this.easyBuffer.delete(this.easyBuffer.length() - size, this.easyBuffer.length());
		}
		return this;
	}

	/**
	 * 从指定位置开始截取,到指定位置结束
	 * 
	 * @param start 起点
	 * @param end   结束点
	 * @return
	 */
	public EasyBuffer delete(int start, int end) {
		this.easyBuffer.delete(start, end);
		return this;
	}

	@Override
	public String toString() {
		return this.easyBuffer.toString();
	}

	public static EasyBuffer newBuffer() {
		return new EasyBuffer();
	}

	public static EasyBuffer newBuffer(Object... objs) {
		return new EasyBuffer().append(objs);
	}

	/**
	 * 将所有参数拼接为字符串返回
	 * 
	 * @param objs
	 * @return
	 */
	public static String newString(Object... objs) {
		return newBuffer(objs).toString();
	}

	/**
	 * 将所有参数拼接为字符串返回
	 * 
	 * @param objs
	 * @return
	 */
	public static String wrapper(Object... objs) {
		return newString(objs);
	}

	/**
	 * 键@值
	 * 
	 * @param key
	 * @param value
	 * @return
	 * @throws IOException
	 */
	public static String at(Object key, Object value) throws IOException {
		if (ObjectUtils.isEmpty(value)) {
			return key.toString();
		}
		if (!value.getClass().getName().contains("java")) {
			value = JSONObject.formatObject(value);
		}
		if (ObjectUtils.isEmpty(key)) {
			return value.toString();
		}
		return EasyBuffer.wrapper(key, "@", value);
	}
}
